package com.headwind.logistics.permission.service.impl;

import com.headwind.logistics.permission.mapper.MenuMapper;
import com.headwind.logistics.permission.mapper.PermsMapper;
import com.headwind.logistics.common.pojo.Menu;
import com.headwind.logistics.permission.service.intl.PermsServiceIntl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 小恶魔
 * @Date 2020-07-19 17:25
 * @Version 1.0
 * @DESCRIPTION 按钮权限业务逻辑层实现类
 */
@Service
public class PermsServiceImpl implements PermsServiceIntl {

    //权限数据访问层
    @Resource
    private PermsMapper permsMapper;

    //菜单数据访问层
    @Resource
    private MenuMapper menuMapper;

    /**
     * 获取全部按钮权限信息
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<Menu> queryAllPermission(Integer page, Integer rows) {

        if (page != null){
            page = (page-1)*rows;
        }

        return permsMapper.queryAllPermission(page,rows);
    }

    /**
     * 添加按钮权限
     * @param menu
     * @param roleIdList
     * @return
     */
    @Override
    @Transactional
    public Integer addPerms(Menu menu, String roleIdList) {

        //添加按钮权限
        Integer count = permsMapper.addPerms(menu);

        if (count > 0){

            String[] ids = roleIdList.split("-");

            for (String id : ids) {
                //循环添加  菜单角色关联表
                menuMapper.addMenuRole(menu.getMenuId(),Long.parseLong(id));
            }


        }

        return count;
    }

    /**
     * 修改按钮权限
     * @param menu
     * @return
     */
    @Override
    public Integer updatePermsById(Menu menu) {

        //修改按钮权限
        Integer count = permsMapper.updatePermsById(menu);

        return count;
    }


}
